#include "Line.h"
#include "ui_Line.h"
#include "Menu.h"
#include "Login.h"
#include <QDebug>
Line::Line(QWidget *parent) :
    MyDialog(parent),
    ui(new Ui::Line)
{
    ui->setupUi(this);
    dialogName=MyDialog::LineDialog;
    patientInformation=new PatientInformation(this);

    QGridLayout *layout = new QGridLayout;
    layout->addWidget(patientInformation);
    this->ui->patient->setLayout(layout);
    connect(&this->callTimer,&QTimer::timeout,this,[=](){
        //发送呼号命令"
        QByteArray array = codec->fromUnicode("<G>"+MyDialog::showPatientInfor).toHex();
        array=QByteArray::fromHex(array);
        qDebug()<<"array:"<<array<<endl;
        write(uart_fd,array.data(),array.size());
    });
    init();
}
Line::~Line()
{
    delete ui;
}

void Line::changeCurrentPatient()
{qDebug()<<"changeCurrentPatient"<<endl;
//    this->ui->patient->ui->name->setText(Menu::patients[currentShowPatientNumber].name);
//    this->ui->patient->ui->sex->setText(Menu::patients[currentShowPatientNumber].sex);
//    this->ui->patient->ui->phone->setText(Menu::patients[currentShowPatientNumber].phone);
//    this->ui->patient->ui->nation->setText(Menu::patients[currentShowPatientNumber].nation);
//    this->ui->patient->ui->age->setText(QString::number(Menu::patients[currentShowPatientNumber].age));
//    this->ui->patient->ui->birth->setText(Menu::patients[currentShowPatientNumber].birth);
//    this->ui->patient->ui->blood->setText(Menu::patients[currentShowPatientNumber].blood);
//    this->ui->patient->ui->id->setText(Menu::patients[currentShowPatientNumber].id);
//    this->ui->patient->ui->illness->setText(Menu::patients[currentShowPatientNumber].illness);
//    this->ui->patient->ui->irritability->setText(Menu::patients[currentShowPatientNumber].irritability);
    this->patientInformation->setUI(Menu::patients[currentShowPatientNumber]);
}

void Line::init()
{qDebug()<<"Menu::patients.size():"<<Menu::patients.size()<<endl;
    QMutexLocker locker(&Menu::patientsLock);
    if(Menu::patients.size()!=0)
    {
        this->ui->noPatient->hide();
        currentShowPatientNumber=0;
        changeCurrentPatient();
    }
    else
    {
        this->patientInformation->hide();
        this->ui->noPatient->show();
    }
}

void Line::on_next_clicked()
{qDebug()<<"currentShowPatientNumber:"<<currentShowPatientNumber<<"    "<<Menu::patients.size()-1<<endl;
    QMutexLocker locker(&Menu::patientsLock);
    if(currentShowPatientNumber<Menu::patients.size()-1)
    {
        currentShowPatientNumber++;
        changeCurrentPatient();
    }
}

void Line::siriWork(MyDialogName name,functionFlag flag)
{
    //呼叫病人
    switch (flag) {
//    case functionFlag::call_yes:

//    break;
    case functionFlag::call_cancel:
        if(name!=this->dialogName)
            this->showPatient_signal("");
        else if(name==this->dialogName)
        {
            this->showPatient_signal("");
            this->ui->call->setText("确定呼叫");
            emit this->siriWork_signal(name,flag);
        }
        this->callTimer.stop();
        this->ui->call->setText("确定呼叫");
        break;
    default:
        if(flag==functionFlag::call_yes)
            emit this->showPatient_signal(MyDialog::showPatientInfor);
        emit this->siriWork_signal(name,flag);
    }
}

void Line::on_last_clicked()
{
    QMutexLocker locker(&Menu::patientsLock);
    if(currentShowPatientNumber>0)
    {
        currentShowPatientNumber--;
        changeCurrentPatient();
    }
}

void Line::on_call_clicked()
{
    QMutexLocker locker(&Menu::patientsLock);
    if(Menu::patients.size()!=0)
    {
        this->ui->call->setText(this->ui->call->text()=="确定呼叫"?"取消呼叫":"确定呼叫");
        if(this->ui->call->text()=="取消呼叫")
        {
            this->callTimer.start(this->callTime);
            QString name,callNumber;
            this->patientInformation->getInformation(name,callNumber);
            MyDialog::showPatientInfor="请"+callNumber+"号"+name+"进入手术室";
            QMetaObject::invokeMethod(&this->callTimer,"timeout",Qt::AutoConnection);
            siriWork(dialogName,functionFlag::call_yes);
        }
        else if(this->ui->call->text()=="确定呼叫")
            siriWork(this->dialogName,call_cancel);
    }
}

void Line::on_finish_clicked()
{
    //QMutexLocker locker(&Menu::patientsLock);
    QString str="7E7E;kind:surgery;function:04;id:%1;phone:%2;";
    Login::myTCPClient->write(str.arg(Menu::patients[currentShowPatientNumber].id).arg(Menu::patients[currentShowPatientNumber].phone),false);
    Menu::patients.removeAt(currentShowPatientNumber);
    init();
}

//void Line::on_pushButton_clicked()
//{
//    showSiri_signal();
//}
